'use strict'

define(['jquery', 'bootstrap', 'datetimepicker', 'datetimepickerCN', 'select2', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
        
    init: function(data) {
        this.create();
        this.changeCoach();
        this.changeTime();
        this.editSchedule();
        this.arrangeTime();
        this.finish();
        this.cancel();
        this.edit();
        this.deleteAction();
        this.createSchedule();
        this.generateSchedule();
    },
    
    create: function() {
        $(document).on('shown.bs.modal', '#schedule-create', function(e){
        	
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })

        })
      },
      
      changeCoach: function() {
          $(document).on('shown.bs.modal', '#change-coach', function(e){
              
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })

          })
        },
        
    changeTime: function(status) {
        $(document).on('shown.bs.modal', '#change-time', function(e){

            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
      },
      
      editSchedule: function() {
          $(document).on('shown.bs.modal', '#edit-schedule', function(e){

              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
          })
        },
        
        arrangeTime: function(status) {
            $(document).on('shown.bs.modal', '#expiry-schedule-time', function(e){

                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
                
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                  beforeSubmit : function(el) {
                    $form.trigger("validate.ajax.submit");
                    return $.bt_validate.result[$.bt_validate.form_id]
                  }
                })
            })
          },
              
          finish: function(status) {
              $(document).on('shown.bs.modal', '#expiry-schedule-finish', function(e){

            	  $(e.currentTarget).find("form").shiftform()
              })
            },
      
      cancel: function(status) {
          $(document).on('shown.bs.modal', '#schedule-cancel', function(e){

              $(e.currentTarget).find("form").shiftform()
          })
        },
        
        edit: function() {
            $(document).on('shown.bs.modal', '#edit', function(e){
          	  
          	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
    	        
    	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
    	        $form.shiftform({
    	          beforeSubmit : function(el) {
    	            $form.trigger("validate.ajax.submit");
    	            return $.bt_validate.result[$.bt_validate.form_id]
    	          }
    	        })

            })
          },
       deleteAction:function() {
            $(document).on('shown.bs.modal', '#delete-action', function(e){
          	  
          	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
    	        
    	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
    	        $form.shiftform({
    	          beforeSubmit : function(el) {
    	            $form.trigger("validate.ajax.submit");
    	            return $.bt_validate.result[$.bt_validate.form_id]
    	          }
    	        })

            })
          },
        
        createSchedule:function() {
              $(document).on('shown.bs.modal', '#create-schedule', function(e){
            	  
            	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
      	        
      	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
      	        $form.shiftform({
      	          beforeSubmit : function(el) {
      	            $form.trigger("validate.ajax.submit");
      	            return $.bt_validate.result[$.bt_validate.form_id]
      	          }
      	        })

              })
            },
        
       generateSchedule:function() {
                $(document).on('shown.bs.modal', '#generate-schedule', function(e){
              	  
              	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
        	        
        	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        	        $form.shiftform({
        	          beforeSubmit : function(el) {
        	            $form.trigger("validate.ajax.submit");
        	            return $.bt_validate.result[$.bt_validate.form_id]
        	          }
        	        })

                })
              }
      
  }
})
